/*
 * Copyright 2009 Zero Separation
 *
 *     This file is part of PDSUtilities.
 *
 *  PDSUtilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PDSUtilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with PDSUtilities.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package com.zero_separation.pds.utilities.referencecollections;

import java.util.Collection;
import java.util.Iterator;

/**
 * This is an abstract implementation of a Collection that will automatically
 * reference and dereference ManagedObjects placed inside. In order to create
 * a new fully featured ReferenceCollection with a different storage mechanism
 * just extend this class and implement the abstract method along with whichever
 * constructors are required.
 *
 * @author Tim Boura - Zero Separation
 */
public abstract class ReferenceAbstractCollection<V, C extends Collection> extends ReferenceAbstract implements Collection<V> {
    /** The version of the serialized form of this class. */
    private static final long serialVersionUID = 1L;

    protected abstract C getInternalCollection();


    public int size() {
        return getInternalCollection().size();
    }

    public boolean isEmpty() {
        return getInternalCollection().isEmpty();
    }

    public boolean contains(Object o) {
        return getInternalCollection().contains(referenceIfNeeded(o));
    }

    public Iterator<V> iterator() {
        return new ReferenceIterator(getInternalCollection().iterator());
    }

    public Object[] toArray() {
        Object[] array = getInternalCollection().toArray();
        for (int i=0;i<array.length;i++)
            array[i] = dereferenceIfNeeded(array[i]);
        return array;
    }

    public <T> T[] toArray(T[] a) {
        T[] array = (T[]) getInternalCollection().toArray(a);
        for (int i=0;i<array.length;i++)
            array[i] = (T)dereferenceIfNeeded(array[i]);
        return array;
    }

    public boolean add(Object e) {
        return getInternalCollection().add(referenceIfNeeded(e));
    }

    public boolean remove(Object o) {
        return getInternalCollection().remove(referenceIfNeeded(o));
    }

    public boolean containsAll(Collection c) {
        for (Object o: c)
            if (!contains(o))
                return false;

        return true;
    }

    public boolean addAll(Collection c) {
        for (Object o: c)
            add(o);
        return c.size()>0;
    }

    public boolean removeAll(Collection c) {
        boolean changed = false;
        for (Object o: c) {
            remove(o);
            changed = true;
        }
        return changed;
    }

    public boolean retainAll(Collection c) {
        boolean changed = false;

        for (Object o: getInternalCollection()) {

            if (!c.contains(dereferenceIfNeeded(o))) {
                changed = true;
                getInternalCollection().remove(o);
            }
        }

        return changed;
    }

    public void clear() {
        getInternalCollection().clear();
    }

}
